package sixkey.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;
import sixkey.Exception.PasswordErrorException;
import sixkey.Exception.UserIsDeletedException;
import sixkey.Exception.UserNameExistException;
import sixkey.Exception.UserNameNotFoundException;
import sixkey.domain.entity.User;
import sixkey.domain.vo.UserVo;
import sixkey.mapper.UserMapper;
import sixkey.service.IUserService;
import sixkey.utils.ResCodeEnum;

/**
 * ClassName:UserServiceImpl
 * Package:sixkey.service.impl
 * Description
 *
 * @Author:@wenxueshi
 * @Create:2023/5/19 - 23:47
 * @Version:v1.0
 */

/**
 * 系统用户类
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    /**
     * 用户注册
     * @param user
     */
    @Override
    public void RegisterUser(User user) throws UserNameExistException {

        //检查是否有相同的用户名
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getUsername,user.getUsername());
        User selectOne = baseMapper.selectOne(wrapper);
        if (selectOne != null){
            //用户名已存在
            throw new UserNameExistException(ResCodeEnum.USERNAME_EXIST);
        }

        //密码+盐+md5加密
        String password = user.getPassword();
        String salt = user.getSalt();
        String MD5Password = DigestUtils.md5DigestAsHex((password + salt).getBytes());
        user.setPassword(MD5Password);

        //添加用户
        baseMapper.insert(user);
    }

    /**
     * 修改用户
     * @param user
     */
    @Transactional
    @Override
    public void updateUser(User user) {
        //密码+盐+md5加密
        String password = user.getPassword();
        String salt = user.getSalt();
        String MD5Password = DigestUtils.md5DigestAsHex((password + salt).getBytes());
        user.setPassword(MD5Password);
        //修改用户
        baseMapper.updateById(user);
    }

    /**
     * 修改用户状态
     * @param id
     * @param state
     */
    @Override
    public void updateUserStatus(Long id, Long state) throws UserIsDeletedException {
        User user = this.getById(id);
        user.setStatus(state);
        baseMapper.updateById(user);
    }

    /**
     * 用户登录
     * @param loginUserVo
     */
    @Override
    public void doLogin(UserVo loginUserVo) throws UserNameNotFoundException, PasswordErrorException {
        //1.根据用户名查询是否有此用户
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getUsername,loginUserVo.getUsername());
        User user = baseMapper.selectOne(wrapper);
        if(user == null){
            //用户名不存在
            throw new UserNameNotFoundException(ResCodeEnum.USERNAME_NOTFOUND);
        }

        //2.密码是否正确
        String salt = user.getSalt();
        String password = loginUserVo.getPassword();
        String MD5Password = DigestUtils.md5DigestAsHex((password + salt).getBytes());
        if(!MD5Password.equals(user.getPassword())){
            throw new PasswordErrorException(ResCodeEnum.LOGIN_ERROR);
        }

        //3.登录
        StpUtil.login(user.getId());
        //往session中设置当前登录用户
        StpUtil.getSession().set("user",user);

    }
}
